import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
from python_ai.common.xcommon import sep

pd.set_option('display.max_rows', None, 'display.max_columns', None, 'display.max_colwidth', 1000,
              'display.expand_frame_repr', False)
plt.rcParams['font.sans-serif'] = ['Simhei']
plt.rcParams['axes.unicode_minus'] = False

sep('data')
df = pd.read_csv(r'../../../../../large_data/ML2/air_data.csv')
# print(df.head())
# print(df.info())
print(df.describe().columns)
print(df.shape)

sep('select notna')
data = df[df['SUM_YR_1'].notna() & df['SUM_YR_2'].notna()]
print(data.shape)
index1 = data['SUM_YR_1'] > 0
index2 = data['SUM_YR_2'] > 0
index3 = (data['SEG_KM_SUM'] != 0) & (data['avg_discount'] != 0)
data = data[index1 | index2 | index3]
print(data.shape)

# "FFP_DATE" 入会注册时间,
# "LOAD_TIME",上次登陆时间
#  "FLIGHT_COUNT",飞行次数
# "SUM_YR_1",第一年消费总额
# "SUM_YR_2",第二年消费总额
# "SEG_KM_SUM",飞行总里程
# "AVG_INTERVAL" , 平均乘坐飞机的时间间隔（对该乘客而言，正常乘坐飞机的间隔）
# "MAX_INTERVAL", 最大乘坐飞机的时间间隔
# "avg_discount" 平均折扣率
data = data[
    ["FFP_DATE", "LOAD_TIME",
     "FLIGHT_COUNT",
     "SUM_YR_1", "SUM_YR_2",
     "SEG_KM_SUM",
     "AVG_INTERVAL", "MAX_INTERVAL",
     "avg_discount"]
]

# 入会时间长度
sep('入会时间长度')
print(data[:5][['FFP_DATE', 'LOAD_TIME']])
data['FFP_DATE'] = pd.to_datetime(data['FFP_DATE'])
data['LOAD_TIME'] = pd.to_datetime(data['LOAD_TIME'])
print(data[:5][['FFP_DATE', 'LOAD_TIME']])
data['入会时间长度'] = data['LOAD_TIME'] - data['FFP_DATE']
print(data[:5][['FFP_DATE', 'LOAD_TIME', '入会时间长度']])

# 每公里票价
sep('平均每公里票价')
data['平均每公里票价'] = (data['SUM_YR_1'] + data['SUM_YR_2']) / data['SEG_KM_SUM']
print(data[:5]['平均每公里票价'])

# 时间间隔差值
sep('时间间隔差值')
data['时间间隔差值'] = data['MAX_INTERVAL'] - data['AVG_INTERVAL']

# pick data
sep('columns')
data = data.rename(columns={'FLIGHT_COUNT': '飞行次数',
                            'SEG_KM_SUM': '总里程',
                            'avg_discount': '平均折扣率'})
data = data[
    ["入会时间长度",
     "飞行次数",
     "平均每公里票价",
     "总里程",
     "时间间隔差值",
     "平均折扣率"]
]
idx = data.index
sep('For testing, %20 == 0')
data = data[idx % 20 == 0]
print(data[:5])
data_columns = data.columns

sep('入会时间长度')
print(data[:5]['入会时间长度'])
data['入会时间长度'] = data['入会时间长度'].astype(np.int64) // (3600 * 24 * 1e9)
print(data[:5]['入会时间长度'])

sep('standard')
from sklearn.preprocessing import StandardScaler
ss = StandardScaler()
data = ss.fit_transform(data)

sep('visualize')
plt.figure(figsize=[12, 6])
spr = 2
spc = 4
spn = 0
from sklearn.cluster import KMeans
from sklearn.metrics import silhouette_score
sse = []
sil = []
labels = []
k_list = range(4, 6+1)
cn = list(data_columns)
cn.append(cn[0])
for i in k_list:
    spn += 1
    plt.subplot(spr, spc, spn, projection='polar')
    plt.title(f'K = {i}')
    model = KMeans(n_clusters=i)
    label = model.fit_predict(data)
    sse.append(model.inertia_)
    sil.append(silhouette_score(data, label))
    labels.append(label)
    col_len = len(cn)
    angles = np.linspace(0, 2 * np.pi, col_len)
    plt.xticks(angles[:-1], cn[:-1])
    ccl = model.cluster_centers_
    for i, c in enumerate(ccl):
        cc = list(c)
        cc.append(cc[0])
        plt.polar(angles, cc, label=f'Class #{i + 1}')
    plt.legend()
spn += 1
plt.subplot(spr, spc, spn)
plt.title('SSE')
plt.plot(k_list, sse, label='Inertial')
plt.legend()

spn += 1
plt.subplot(spr, spc, spn)
plt.title('Silhouette')
plt.plot(k_list, sil, label='Silhouette')
plt.legend()

spn += 1
ax = plt.subplot(spr, spc, spn)
plt.title('Count')
plt.axis('off')
labels_s = pd.Series(labels[1]).value_counts()
labels_s.index = ['一般保持客户', '易流失客户', '低价值客户', '重点发展客户', '重点保持客户']
labels_s.plot(kind='pie', autopct='%.2f%%', ax=ax)

spn += 1
ax = plt.subplot(spr, spc, spn)
plt.title('Count')
labels_s.plot(kind='bar', ax=ax)

# Finally show all drawings.
plt.show()
